package com.laker.admin.module.comm.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.laker.admin.framework.aop.metrics.Metrics;
import com.laker.admin.framework.aop.trace.LakerTrace;
import com.laker.admin.framework.model.ResultTree;
import org.springframework.web.bind.annotation.RequestMapping;
import com.laker.admin.module.comm.entity.CommType;
import com.laker.admin.module.comm.service.ICommTypeService;
import org.springframework.web.bind.annotation.RestController;
import com.laker.admin.framework.model.Response;
import com.laker.admin.framework.model.PageResponse;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author laker
 * @since 2024-03-20
 */
@RestController
@RequestMapping("/comm/type")
@Metrics
@LakerTrace
public class CommTypeController {
    @Autowired
    ICommTypeService commTypeService;

    @GetMapping
    @ApiOperation(value = "分页查询")
    public PageResponse pageAll(@RequestParam(required = false, defaultValue = "1") long page,
                                @RequestParam(required = false, defaultValue = "10") long limit,
                                String keyWord) {
        Page roadPage = new Page<>(page, limit);
        LambdaQueryWrapper<CommType> queryWrapper = new QueryWrapper().lambda();

        queryWrapper.eq(CommType::getDelFlag, 0).and(StrUtil.isNotBlank(keyWord), i -> i.
                        like(StrUtil.isNotBlank(keyWord), CommType::getCommTypeName, keyWord)
                        .or().like(StrUtil.isNotBlank(keyWord), CommType::getCommTypeName, keyWord));

        Page pageList = commTypeService.page(roadPage, queryWrapper);
        return PageResponse.ok(pageList.getRecords(), pageList.getTotal());
    }

    @PostMapping
    @ApiOperation(value = "新增或者更新")
    public Response saveOrUpdate(@RequestBody CommType param) {
        return Response.ok(commTypeService.saveOrUpdate(param));
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "根据id查询")
    public Response get(@PathVariable Long id) {
        return Response.ok(commTypeService.getById(id));
    }

    @DeleteMapping("/{id}")
    @ApiOperation(value = "根据id删除")
    public Response delete(@PathVariable Long id) {
        return Response.ok(commTypeService.removeById(id));
    }

    @DeleteMapping("/batch/{ids}")
    @ApiOperation(value = "根据批量删除ids删除")
    public Response batchRemove(@PathVariable Long[] ids) {
        return Response.ok(commTypeService.removeByIds(CollUtil.toList(ids)));
    }

    @GetMapping("/tree")
    public ResultTree tree(CommType param) {
        List<CommType> data = commTypeService.list(Wrappers.<CommType>lambdaQuery()
                .eq(CommType::getDelFlag, 0)
                .orderByAsc(CommType::getCreatedTime));
        return ResultTree.data(data);
    }
}
